﻿#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 ZHENG Zhong <heavyzheng nospam-at gmail D0T com>
# - http://heavyz.blogspot.com/
# - http://buggarden.blogspot.com/
#
# Created on 2008-05-06.
# $Id: views.py 307 2008-12-29 13:48:00Z heavyzheng $
#

import logging

from django.template import loader
from django.template import Context
from django.http import HttpRequest
from django.http import HttpResponse
from django.http import HttpResponseRedirect

from google.appengine.api import users

#---------------------------------------------------------------------------------------------------
# Sub-webapp homepages
#---------------------------------------------------------------------------------------------------

def _afcp_home(request):
    return HttpResponseRedirect("/afcp/")

def _default_home(request):
    return _afcp_home(request)


HOME_HANDLER_MAPPINGS = {  "afcp" : _afcp_home, }

SERVER_HANDLER_MAPPINGS = { "www.afcp-paristech.org"    : _afcp_home,
                            "beta.afcp-paristech.org"   : _afcp_home,
                            "afcp.appspot.com"          : _afcp_home, }

#---------------------------------------------------------------------------------------------------
# Homepage handler
#---------------------------------------------------------------------------------------------------

def home(request):
    home = request.REQUEST.get("home")
    handler = HOME_HANDLER_MAPPINGS.get(home, None)
    if handler is not None:
        return handler(request)
    else:
        server = request.META.get("SERVER_NAME")
        handler = SERVER_HANDLER_MAPPINGS.get(server, None)
        if handler is not None:
            return handler(request)
        else:
            return _default_home(request)

#---------------------------------------------------------------------------------------------------
# The 404 not-found page
#---------------------------------------------------------------------------------------------------

def not_found(request):
    """Renders the 404 not-found page.
    """
    
    logging.info("The requested page '%s' does not exist." % request.path)
    ctxt_dict = { "url": request.path, }
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("not_found.html")
    return HttpResponse(tmpl.render(ctxt))




